package com.wollo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.wollo.domain.AjaxResult;
import com.wollo.domain.AliPhoneCode;
import com.wollo.utils.PhoneUtils;
import com.wollo.utils.SendSmsAli;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

/**
 * //
 * //   █████▒█    ██  ▄████▄   ██ ▄█▀       ██████╗ ██╗   ██╗ ██████╗
 * // ▓██   ▒ ██  ▓██▒▒██▀ ▀█   ██▄█▒        ██╔══██╗██║   ██║██╔════╝
 * // ▒████ ░▓██  ▒██░▒▓█    ▄ ▓███▄░        ██████╔╝██║   ██║██║  ███╗
 * // ░▓█▒  ░▓▓█  ░██░▒▓▓▄ ▄██▒▓██ █▄        ██╔══██╗██║   ██║██║   ██║
 * // ░▒█░   ▒▒█████▓ ▒ ▓███▀ ░▒██▒ █▄       ██████╔╝╚██████╔╝╚██████╔╝
 * //  ▒ ░   ░▒▓▒ ▒ ▒ ░ ░▒ ▒  ░▒ ▒▒ ▓▒       ╚═════╝  ╚═════╝  ╚═════╝
 * //  ░     ░░▒░ ░ ░   ░  ▒   ░ ░▒ ▒░
 * //  ░ ░    ░░░ ░ ░ ░        ░ ░░ ░
 * //           ░     ░ ░      ░  ░
 * //                 ░
 *
 * @author Java song
 * @date 2024/9/16 上午11:08
 * @description
 **/
@RestController
public class LoginPhoneController {
    @Autowired
    private PhoneUtils phoneUtils;

    @PostMapping("/getPhoneCode")
    public AjaxResult getPhoneCode(String phone, HttpSession session) throws Exception {
        // TODO 判断手机用户是否存在
        if (!phoneUtils.checkPhone(phone)){
            return AjaxResult.error("请检查您的手机号是否正确");
        }
        // TODO 生成6位随机数并返回
        String code = phoneUtils.generateRandomCode();
        // TODO 将验证码存入session
        System.out.println(code);
        session.setAttribute("sysCode",code);
        // TODO 调用阿里云平台发送验证码
        SendSmsAli.sendPhoneCode(phone,new ObjectMapper().writeValueAsString(new AliPhoneCode(code)));
        // TODO 返回成功
        return AjaxResult.ok();
    }

}
